package com.csun.cmny.provider.service.impl;

import com.csun.cmny.provider.dao.CacOldmanMapper;
import com.csun.cmny.provider.dto.CacOldManDto;
import com.csun.cmny.provider.dto.levDto;
import com.csun.cmny.provider.model.dto.OmcOldManDto;
import com.csun.cmny.provider.model.vo.CacOldManVo;
import com.csun.cmny.provider.model.vo.CareTypeCountVo;
import com.csun.cmny.provider.pojo.CacOldman;
import com.csun.cmny.provider.service.CacOldmanMinorService;
import com.csun.cmny.provider.service.CacOldmanService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.provider.vo.CacEvaluateOldManVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("CacOldmanService")
public class CacOldmanServiceImpl implements CacOldmanService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    CacOldmanMapper cacOldmanMapper;

    @Resource
    private CacOldmanMinorService cacOldmanMinorService;

    @Resource
    private UacRpcService uacRpcService;

    @Override
    public int insert(CacOldman cacOldman) {
        int n = cacOldmanMapper.insertSelective(cacOldman);
        return n > 0 ? cacOldman.getCacId() : n;

    }

    @Override
    public int update(CacOldman cacOldman) {
        return cacOldmanMapper.updateByPrimaryKeySelective(cacOldman);
    }

    @Override
    public int deleteByCacId(Integer cacId) {
        return cacOldmanMapper.deleteByPrimaryKey(cacId);
    }

    @Override
    public CacOldman selectByCacId(Integer cacId) {
        return cacOldmanMapper.selectByPrimaryKey(cacId);
    }

    @Override
    public List<Map<String, Object>> showAll(Map<String, Object> queryMap) {
        return cacOldmanMapper.showAll(queryMap);
    }

    @Override
    public List<Map<String, Object>> getCacOldmanInfo(Map queryMap) {
        return cacOldmanMapper.getCacOldmanInfo(queryMap);
    }

    @Override
    public Integer deleteByOldmanId(Integer oldmanId) {
        //删除老人评估信息
        int n = cacOldmanMapper.deleteByOldmanId(oldmanId);
        if (n > 0) {
            //删除老人和小项关系
            int m = cacOldmanMinorService.deleteByOldmanId(oldmanId);
            if (m < 1) {
                logger.info("删除老人评估记录时删除小项失败");
            }
        }
        return n;
    }

    /**
     * 根据评估状态和老人姓名查询评估记录
     *
     * @param cacOldManDto
     * @return
     */
    @Override
    public List<CacEvaluateOldManVo> showByType(CacOldManDto cacOldManDto) {
        List<CacEvaluateOldManVo> resultList = new ArrayList<>();
        //远程查询老人信息
        OmcOldManDto omcOldManDto = new OmcOldManDto();
        omcOldManDto.setGroupId(cacOldManDto.getGroupId());
        omcOldManDto.setOldName(cacOldManDto.getOldName());
        if (null != cacOldManDto && cacOldManDto.getEvaluateType() == 1) {//evaluateType=1时 初评 -查询待入院的老人
            //查询已有初评记录的老人
            /**
             * @author  chisj
             * @date    2019-11-15
             * @desc    修复bug #3560 已出院的老人可以再入院，可多次初评
             */
//            List<CacEvaluateOldManVo> existList = cacOldmanMapper.queryCac(cacOldManDto);
//            if (null != existList && existList.size() > 0) {//获取已初评老人的记录
//                List<Long> unList = new ArrayList<>();
//                for (CacEvaluateOldManVo vo : existList) {
//                    unList.add(vo.getOldmanId());
//                }
//                omcOldManDto.setUnList(unList);
//            }
            /*
            * 2019-11-11 修改
            * 查询老人状态改为查询【待评估】状态的老人
            * */
//            omcOldManDto.setStatus(0);//待入院状态
            omcOldManDto.setStatus(1);
            List<CacOldManVo> cacOldmanList = uacRpcService.showCacOldMan(omcOldManDto);
            if (null != cacOldmanList && cacOldmanList.size() > 0) {
                for (CacOldManVo cacOldManVo : cacOldmanList) {//进行老人信息填充
                    CacEvaluateOldManVo evaluateOldManVo = new CacEvaluateOldManVo();
                    evaluateOldManVo.setOldManNo(cacOldManVo.getOldManNo());//老人编号
                    evaluateOldManVo.setOldName(cacOldManVo.getOldName());//老人姓名
                    evaluateOldManVo.setSex(cacOldManVo.getSex());//老人性别
                    evaluateOldManVo.setOldIdCard(cacOldManVo.getOldIdCard());//老人身份证
                    evaluateOldManVo.setOldmanId(cacOldManVo.getOldManId());
                    resultList.add(evaluateOldManVo);
                }
            }
        } else if (null != cacOldManDto && cacOldManDto.getEvaluateType() == 5) {//evaluateType=5时为及时评估 -查询所有入院老人
            //经过测试讨论,及时评估时,查询待缴费、已入院和试入住的老人
            /*
             * 2019-11-18
             * 经测试讨论 去掉待缴费的查询
             * */
            List<Integer> statusList = new ArrayList<>();
//            statusList.add(2);//待缴费
            statusList.add(3);//已入院
            statusList.add(5);//试入住
            statusList.add(7);//请假中
            statusList.add(8);//调房调剂中
            statusList.add(9);//出院中
            omcOldManDto.setStatusList(statusList);
            //omcOldManDto.setStatus(3);//入院状态
            List<CacOldManVo> cacOldmanList = uacRpcService.showCacOldMan(omcOldManDto);
            if (null != cacOldmanList && cacOldmanList.size() > 0) {
                for (CacOldManVo cacOldManVo : cacOldmanList) {//进行老人信息填充
                    CacEvaluateOldManVo evaluateOldManVo = new CacEvaluateOldManVo();
                    evaluateOldManVo.setOldManNo(cacOldManVo.getOldManNo());//老人编号
                    evaluateOldManVo.setOldName(cacOldManVo.getOldName());//老人姓名
                    evaluateOldManVo.setSex(cacOldManVo.getSex());//老人性别
                    evaluateOldManVo.setOldIdCard(cacOldManVo.getOldIdCard());//老人身份证
                    evaluateOldManVo.setOldmanId(cacOldManVo.getOldManId());
                    resultList.add(evaluateOldManVo);
                }
            }
        } else {//evaluateType不为1时 查询范围是查询半年或半年以上没有评估过的老人
            //查询出评估记录中最近评估的在院老人
            CacOldManDto dto = new CacOldManDto();
            dto.setGroupId(cacOldManDto.getGroupId());
            List<CacEvaluateOldManVo> list = cacOldmanMapper.showByType(dto);
            if (null != list && list.size() > 0) {//填充老人信息
                List<Long> oldManIdList = new ArrayList<>();
                for (CacEvaluateOldManVo evaluateOldManVo : list) {//获取老人ID
                    oldManIdList.add(evaluateOldManVo.getOldmanId());
                }
                if (oldManIdList.size() > 0) {
                    //远程查询老人信息
                    omcOldManDto.setOldManIdList(oldManIdList);
                    //经过测试讨论,常规评估时,查询待缴费、已入院和试入住的老人
                    /*
                    * 2019-11-18
                    * 经测试讨论 去掉待缴费的查询
                    * */
                    List<Integer> statusList = new ArrayList<>();
//                    statusList.add(2);//待缴费
                    statusList.add(3);//已入院
                    statusList.add(5);//试入住
                    statusList.add(7);//请假中
                    statusList.add(8);//调房调剂中
                    statusList.add(9);//出院中
                    omcOldManDto.setStatusList(statusList);
                    List<CacOldManVo> cacOldmanList = uacRpcService.showCacOldMan(omcOldManDto);
                    if (null != cacOldmanList && cacOldmanList.size() > 0) {
                        for (CacOldManVo cacOldManVo : cacOldmanList) {//进行老人信息填充
                            for (CacEvaluateOldManVo evaluateOldManVo : list) {
                                if (cacOldManVo.getOldManId().longValue() == evaluateOldManVo.getOldmanId().longValue()) {
                                    evaluateOldManVo.setOldManNo(cacOldManVo.getOldManNo());//老人编号
                                    evaluateOldManVo.setOldName(cacOldManVo.getOldName());//老人姓名
                                    evaluateOldManVo.setSex(cacOldManVo.getSex());//老人性别
                                    evaluateOldManVo.setOldIdCard(cacOldManVo.getOldIdCard());//老人身份证
                                    resultList.add(evaluateOldManVo);
                                }
                            }
                        }
                    }
                }
            }

        }
        return resultList;
    }

    /**
     * 分页查询评估记录--武昌版
     *
     * @param cacOldManDto
     * @return
     */
    @Override
    public List<CacEvaluateOldManVo> showAllWithPage(CacOldManDto cacOldManDto) {

        /**
         * @desc    修复bug #3332
         * @data    2019-10-09
         * @author  chisj
         */
        // 1、查询符合条件的老人列表
        OmcOldManDto omcOldManDto = new OmcOldManDto();
       // omcOldManDto.setGroupId(cacOldManDto.getGroupId());
        omcOldManDto.setOldName(cacOldManDto.getOldName());
        omcOldManDto.setOldManId(cacOldManDto.getOldManId());

        //远程查询老人信息
        List<CacOldManVo> cacOldmanList = uacRpcService.showCacOldMan(omcOldManDto);
        Map<Long, CacOldManVo> cacOldmanVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(cacOldmanList)) {
            // 抽出oldmanId和oldmanInfo
            List<Long> oldmanIdList = Lists.newArrayList();
            for (CacOldManVo cov : cacOldmanList) {
                oldmanIdList.add(cov.getOldManId());
                cacOldmanVoMap.put(cov.getOldManId(), cov);
            }

            cacOldManDto.setOldmanIdList(oldmanIdList);
        } else {    // 未查询到老人
            if (PublicUtil.isNotEmpty(omcOldManDto.getOldName())) {
                // 根据老人姓名模糊搜索，但是未查询到老人信息
                return Lists.newArrayList();
            }
        }

        // 2、查询符合调级的老人评估记录
        List<CacEvaluateOldManVo> list = cacOldmanMapper.showAllWithPage(cacOldManDto);

        for (CacEvaluateOldManVo ceov : list) {
            CacOldManVo cacOldManVo = cacOldmanVoMap.get(ceov.getOldmanId());
            if (PublicUtil.isNotEmpty(cacOldManVo)) {
                ceov.setOldManNo(cacOldManVo.getOldManNo());//老人编号
                ceov.setOldName(cacOldManVo.getOldName());//老人姓名
                ceov.setSex(cacOldManVo.getSex());//老人性别
                ceov.setOldIdCard(cacOldManVo.getOldIdCard());//老人身份证
            }
        }

        return list;
    }

    @Override
    public List<CareTypeCountVo> queryOldmanNumGroupByCareType(List<Integer> oldmanIdList) {
        List<CareTypeCountVo> vos = cacOldmanMapper.queryOldmanNumGroupByLevelCareId(oldmanIdList);
        for (CareTypeCountVo vo : vos) {
            String levelCareName=cacOldmanMapper.selectlevelCareNameByNewCreateTime(vo.getLevelCareId());
            vo.setLevelCareName(levelCareName);
        }
        return vos;
    }

    @Override
    public List<Integer> queryNeedDailyEvaluateOldmanIdList(Long groupId) {
        return cacOldmanMapper.queryNeedDailyEvaluateOldmanIdList(groupId);
    }

    @Override
    public List<CacEvaluateOldManVo> listTheLastestEvaluateRecordByOldmanIdList(CacOldManDto cacOldManDto) {


        return cacOldmanMapper.newlistTheLastestEvaluateRecordByOldmanIdList(cacOldManDto);
    }

    @Override
    public List<CacEvaluateOldManVo> listTheLastestEvaluateRecord(CacOldManDto cacOldManDto) {
        // 1、查询符合条件的老人列表
        OmcOldManDto omcOldManDto = new OmcOldManDto();
        omcOldManDto.setGroupId(cacOldManDto.getGroupId());
        omcOldManDto.setOldName(cacOldManDto.getOldName());
        omcOldManDto.setOldIdCard(cacOldManDto.getOldIdCard());
        omcOldManDto.setOldManId(cacOldManDto.getOldManId());

        //远程查询老人信息
        List<CacOldManVo> cacOldmanList = uacRpcService.showCacOldMan(omcOldManDto);

        Map<Long, CacOldManVo> cacOldmanVoMap = Maps.newHashMap();
        if (PublicUtil.isNotEmpty(cacOldmanList)) {
            // 抽出oldmanId和oldmanInfo
            List<Long> oldmanIdList = Lists.newArrayList();
            for (CacOldManVo cov : cacOldmanList) {
                oldmanIdList.add(cov.getOldManId());
                cacOldmanVoMap.put(cov.getOldManId(), cov);
            }

            cacOldManDto.setOldmanIdList(oldmanIdList);
        } else {    // 未查询到老人
            if (PublicUtil.isNotEmpty(omcOldManDto.getOldName()) || omcOldManDto.getOldName().equals("")) {
                // 根据老人姓名模糊搜索，但是未查询到老人信息
                return Lists.newArrayList();
            }
        }

        // 2、查询符合调级的老人评估记录
//        List<CacEvaluateOldManVo> list = cacOldmanMapper.showAllWithPage(cacOldManDto);
        /*
         * 2020-07-30
         * xcr
         * 二次入院
         * 查询最新一条评估记录
         * */
        List<CacEvaluateOldManVo> list = listTheLastestEvaluateRecordByOldmanIdList(cacOldManDto);
        for (CacEvaluateOldManVo ceov : list) {
            CacOldManVo cacOldManVo = cacOldmanVoMap.get(ceov.getOldmanId());
            if (PublicUtil.isNotEmpty(cacOldManVo)) {
                ceov.setOldManNo(cacOldManVo.getOldManNo());//老人编号
                ceov.setOldName(cacOldManVo.getOldName());//老人姓名
                ceov.setSex(cacOldManVo.getSex());//老人性别
                ceov.setOldIdCard(cacOldManVo.getOldIdCard());//老人身份证
            }
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> selectCacOldManAll() {
        return cacOldmanMapper.selectCacOldManAll();
    }

    @Override
    public Integer updateLevelCare(List<Map<String, Object>> updateDtos) {

        Integer update=0;
        System.out.println("修改长度"+updateDtos.size());
        for (Map<String, Object> updateDto : updateDtos) {
            Integer cacId = (Integer) updateDto.get("cacId");
            CacOldman cacOldman = cacOldmanMapper.selectByPrimaryKey(cacId);
            if (PublicUtil.isEmpty(cacOldman.getLevelCareId())){
                System.out.println("修改"+cacId);
                update+=cacOldmanMapper.updateLevelCare(updateDto);
            }
        }

        return update;
    }
}
